﻿using Prism.Events;

namespace Com.Lancetinc.Evs.Events
{
    /// <summary>
    /// 事件总线相关
    /// 用于不同ViewModel之间通信
    /// </summary>
    public sealed class EventBus
    {
        private IEventAggregator _eventAggregator;

        public static TEventType GetEvent<TEventType>() where TEventType : EventBase, new()
        {
            return Instance.EventAggregator.GetEvent<TEventType>();
        }

        /// <summary>
        /// 取得一个Event
        /// 注意相同的T调用两次取得的是同一个对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static PubSubEvent<T> GetPubSubEvent<T>()
        {
            return GetEvent<PubSubEvent<T>>();
        }

        private EventBus()
        {
        }

        internal static EventBus Instance { get; } = new ();

        internal IEventAggregator EventAggregator => _eventAggregator ??= new EventAggregator();
    }
}
